﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for FeatureGUI.xaml
    /// </summary>
    
    public partial class FeatureGUI : UserControl
    {
        private eKanbanWPF.Model.Feature feature;
        public event EventHandler Changed;

        public delegate void PositionChangedHandler(FeatureGUI featureGui);
        public event PositionChangedHandler PositionChanged;

        public eKanbanWPF.Model.Feature Feature { 
            get { return feature; }
            set
            {
                if (feature != null)
                {
                    feature.Changed -= feature_Changed;
                }
                feature = value;
                if (feature != null)
                {
                    feature.Changed += feature_Changed;
                }
                feature_Changed(value); ;
            }
        }

        private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, true);
            ((Control)sender).Focus();
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, false);
            if (feature != null)
            {
                feature.Width = this.Width;
                feature.Height = this.Height;

                feature.Left = Canvas.GetLeft(this);
                feature.Top = Canvas.GetTop(this);
                
            }

        }

        void feature_Changed(eKanbanWPF.Model.Feature feature)
        {
            if (feature != null)
            {
                Width = feature.Width;
                Height = feature.Height;
                Canvas.SetTop(this, feature.Top);
                Canvas.SetLeft(this, feature.Left);

                lblName.Text = feature.Name;
                //lblPriority.Text = feature.Priority.ToString();
                StringBuilder s = new StringBuilder();
                for (int i = 0; i < feature.Priority; i++)
                {
                    s.Append("«");
                }
                lblPriority.Text = s.ToString();
                txtDescription.Text = feature.Description;
            }
            if (Changed != null)
            {
                Changed.Invoke(this, null);
            }
        }

        public FeatureGUI()
        {
            InitializeComponent();
        }

        private void UserControl_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (feature == null)
                Feature = new Model.Feature();

            feature.Top = Canvas.GetTop(this);
            feature.Left = Canvas.GetLeft(this);

            var r = FrmFeature.Show(feature);
            if (r == true)
            {
                feature.Name = FrmFeature.FeatureName;
                feature.Description = FrmFeature.FeatureDescription;
                feature.Priority = Decimal.ToInt32(FrmFeature.FeaturePriority);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }
            }
        }

        private void UserControl_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (feature != null)
            {
                bool ck = false;
                if (feature.Left != Canvas.GetLeft(this) || feature.Top != Canvas.GetTop(this))
                {
                    ck = true;
                }

                feature.Width = this.Width;
                feature.Height = this.Height;

                feature.Left = Canvas.GetLeft(this);
                feature.Top = Canvas.GetTop(this);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }

                if (ck)
                {
                    PositionChanged.Invoke(this);
                }
            }
        }
    }
}
